<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Box extends Model
{
    public $table = 'boxs';
    use SoftDeletes;

    protected $fillable = [
    	'id',
        'number',
        'time',
        'username',
        'mobile',
        'life_id',
        'order_id',
    ];

    public function getDeliveryAtAttribute()
    {
        return date('Y-m-d',strtotime($this->attributes['delivery_at']));
    }

    public function getUserName($wechat_id)
    {
        $user = UsersWechat::find($wechat_id);
        return $user ? $user->nickname : '';
    }

    public function getThingAttribute()
    {
        return unserialize($this->attributes['thing']);
    }

    public function getColorAttribute()
    {
        return unserialize($this->attributes['color']);
    }


    public function BoxOrder()
    {
        return $this->belongsTo('App\Models\BoxOrder','box_id','order_id');
        // return $this->belongsToMany(Order::class,'id','order_id');
    }



    public  function Address()
    {
        return $this->hasOne('App\Models\Address','id','address_id');
    }


    public  function collocation()
    {
        return $this->hasMany('App\Models\Collocation','box_id','id');
    }

    public function getPhotoAttribute()
    {
        
        $photo = $this->attributes['photo'] == ''? '': url($this->attributes['photo']);
        return $photo;
    }

    public static function add($orderId,$wechatId){
        $date=date("Y-m-d",strtotime("+10 day"));
        $user = UsersWechat::find($wechatId);
        $profile = UserProfile::where('wechat_id',$wechatId)->first();
        if (!$profile) {
            $profile = new UserProfile;
        }
        $user_address_id = UserAddress::select('id')->where('default',1)->value('id');
        for( $i= 0;$i < 6; $i++){
            $n  = Box::where('wechat_id',$wechatId)->count('id');
            if ($i>0) {
                $date = date("Y-m-d",strtotime("+2 month",strtotime($date)));
            }
            $box = new Box();
            if ($i > 3) {
                $box->type= 1;//赠送盒子
            }else{
                $box->type= 0;
            }
            $n = intval($n)+1;
            $box->wechat_id= $wechatId;
            $box->order_id =$orderId;
            $box->number= $user->number.'-'.$n.'-'.date('YmdHis');
            if ($profile) {
                $box->sex= $profile->sex;
                $box->age= $profile->age;
                $box->height= $profile->height;
                $box->weight= $profile->weight;
                $box->jacket_size= $profile->jacket_size;
                $box->pants_size= $profile->pants_size;
                $box->shoes_size= $profile->shoes_size;
                $box->skin_color= $profile->skin_color;
                $box->figure= $profile->figure;
                $box->color= $profile->color;
                $box->life= $profile->life;
                $box->photo= $profile->photo;
                $box->occupation= $profile->occupation;
                $box->position= $profile->position;
                $box->birthday= $profile->birthday;
                $box->thing= $profile->thing;
                $box->other= $profile->other;
            }else{
                $box->color = serialize([]);
                $box->thing = serialize([]);
            }

            $box->status = 1;
            $box->delivery_at = $date;
            BoxAddress::boxCopyAddress(['wechat_id'=>$wechatId,'box_id'=>$box->id,'user_address_id'=>$user_address_id,'type'=>'box']);
            $stylist_id = Stylist::orderBy(\DB::raw('RAND()'))->take(1)->value('id');
            $box->stylist_id = $stylist_id ? $stylist_id :0;
            $box->save();
        }
        return true;
    }



    public  function getStatusName(){
        $status = $this->getAttributeValue('status');
        switch ($status) {
            case 1:
                return '待搭配';
                break;
            case 2:
                return '搭配中';
                break;
            case 3:
                return '待发货';
                break;
            case 4:
                return '待收货';
                break;
            case 5:
                return '待评论';
                break;
            case 6:
                return '完成';
                break;
            default:
                return '未知状态';
                break;
        }
    }
}
